package com.xiong.jdbc;

import java.sql.*;

public class DBSql {

    private Connection connection;

    public Connection getConnection(){
        return this.connection;
    }

    public int executeUpdate(String sql,Object ... args){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            // 连接数据库
            connection = getConnection();
            // 创建 PreparedStatement 对象，并设置参数
            statement = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                statement.setObject(i+1, args[i]);
            }
            // 执行更新操作
            int num=statement.executeUpdate();
            return num;
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            try {
                // 关闭资源
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }




    public int insertGeneratedKey(Connection connection,String sql,Object ... args)  {
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try {
            statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            for (int i = 0; i < args.length; i++) {
                statement.setObject(i + 1, args[i]);
            }
            int num=statement.executeUpdate();
            resultSet = statement.getGeneratedKeys();
            if (resultSet.next()) {
                return resultSet.getInt(1); // 返回生成的主键 ID
            } else {
                if(num>0){
                    return num;
                }
                return -1; // 插入失败，返回 -1
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return -1;
    }

    public int insertGeneratedKey(String sql,Object ... args)  {
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try {
            connection = getConnection();
            statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            for (int i = 0; i < args.length; i++) {
                statement.setObject(i + 1, args[i]);
            }
            int num=statement.executeUpdate();
            resultSet = statement.getGeneratedKeys();
            if (resultSet.next()) {
                return resultSet.getInt(1); // 返回生成的主键 ID
            } else {
                if(num>0){
                    return num;
                }
                return -1; // 插入失败，返回 -1
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            try {
                if(connection != null){
                    connection.close();
                }
                if(statement != null){
                    statement.close();
                }
                if(resultSet != null){
                    resultSet.close();
                }
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return -1;
    }
}
